// 课件包是内容
import { request } from '@/api/request';
import { getOrgEntType } from '@/components/EntTypeSelect/utils';
import { ShelfProducts } from '../interface';
import Shelf from '../Shelf';

/**
 * @desc 未上架的课件包返回结果
 */
interface UnShelfPaper {
  coursewareId: string;
  name: string;
  coursewareType: string;
  picUrl: string;
  classId: string;
  createTime: string;
  updateTime: string;
  totalScore: number;
  passScore: number;
  scoreInfo: string;
  shelvesCopy: number;
  isShelves: number;
  shelvesPaper: number;
  copyId: string;
  faceImg: string;
  aiLearnClassification?: number;
}

/**
 * @desc 已经上架的课件包的返回结果
 */
type OnShelfPaper = {
  guid: string;
  region: string;
  entTypeCode: string;
  trade: string;
  isCharge: number;
  regionName: string;
  name: string;
  classId: string;
  creatTime: string;
  faceImg: string;
  tradeName: string;
  aiLearnClassification?: number;
};

class Paper implements ShelfProducts {
  isOnShelf: boolean;
  name: string;
  guid: string;
  origin: Partial<UnShelfPaper & OnShelfPaper>;
  /**
   * 企业类型解析结果
   */
  entTypeCodeText?: string;
  /**
   * @desc 是否收费
   */
  isChargeText?: string;
  aiLearnClassification?: number;

  constructor(guid: string, name: string, isOnShelf: boolean, origin: Partial<UnShelfPaper & OnShelfPaper>) {
    this.guid = guid;
    this.name = name;
    this.isOnShelf = isOnShelf;
    this.origin = origin;
    // 自己计算得到的
    this.entTypeCodeText = getOrgEntType(origin.entTypeCode);
    this.isChargeText = ['否', '是'][origin.isCharge ?? -1];
    this.aiLearnClassification = origin.aiLearnClassification;
  }

  /**
   * @desc 创建时间/发布时间
   */
  get createTime() {
    return;
  }

  get updateTime() {
    return this.origin.updateTime;
  }

  /**
   * @desc 分类子级id
   */
  get classId() {
    return this.origin.classId;
  }

  /**
   * @desc 分类父级id
   */
  get parClassId() {
    return;
  }

  /**
   * @desc 封面图
   */
  get cover() {
    return this.origin.faceImg;
  }

  /**
   * @desc 区域
   */
  get area() {
    return this.origin.regionName;
  }

  /**
   * @desc 行业
   */
  get trade() {
    return this.origin.tradeName;
  }

  static shelf = new Shelf(4);

  /**
   * @desc 未发布列表
   */
  static async unShelfList(pageNum: number, pageSize: number, params: Record<string, unknown>) {
    const { data } = await request<{ list: UnShelfPaper[]; total: number }>({
      url: '/safe/api/platform/courseware/paperList',
      params: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => new Paper(v.coursewareId, v.name, false, v)),
      total: data.total,
    };
  }

  /**
   * @desc 已发布列表
   */
  static async onShelfList(pageNum: number, pageSize: number, params: Record<string, unknown>) {
    const { data } = await request<{ list: any[]; total: number }>({
      url: '/safe/api/shelves/paperList',
      data: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => new Paper(v.guid, v.name, true, v)),
      total: data.total,
    };
  }

  /**
   * @desc 创建课件包,如果课件包已经存在则编辑课件包.目前只有新增,没有编辑
   */
  static async createOrUpdate(params: { [key: string]: unknown }) {
    await request({
      url: '/safe/api/platform/courseware/create',
      data: params,
    });
    return true;
  }

  static async createByCopy(params: { guid: string; classId: string }) {
    await request({
      url: '/safe/api/platform/courseware/shelvesPaper',
      params,
    });
  }

  /**
   * @desc 删除课件包
   */
  static async delete(coursewareId: string) {
    await request({
      url: '/safe/api/platform/courseware/deletePaper',
      params: { coursewareId },
    });
    return true;
  }
}

export default Paper;
